﻿using Inaction.Rule;
using System;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.Database
{
    public partial class DbConnecteForm : Form
    {
        public DbConnecteForm()
        {
            InitializeComponent();
            LoadConfigedSqlType();
        }
        private void LoadConfigedSqlType()
        {
            sqlTypeBindingSource.DataSource = Rule.EnumDescriptionList.Query<SqlType>(DbProviderManager.Providers.Select(p => p.SqlType).ToArray());
            comboBox.SelectedValue = AppConfig.Instance.SqlType;
            tbConnectionString.Text = AppConfig.Instance.ConnectionString;
        }

        private void btnConnecte_Click(object sender, EventArgs e)
        {
            AppConfig.Instance.SqlType = (SqlType)comboBox.SelectedValue;
            AppConfig.Instance.ConnectionString = tbConnectionString.Text.Trim();
            AppConfig.Instance.Save();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnDbConfig_Click(object sender, EventArgs e)
        {
            var f = new DbConfigForm();
            f.ShowDialog();
            LoadConfigedSqlType();
        }
    }
}